<template>
	<view>
		<!-- 搜索框 -->
		<view class="search-cont">
			<view class="search">
				<input type="text" placeholder-class="inputclass" confirm-type="search" focus="true" placeholder="请输入关键字" @confirm="onKeyInput" v-model="searchdata" />
			</view>
			<view class="search-code" @click="getsearchdata">搜索</view>
		</view>

		<!-- 搜索历史 -->
		<view class="search-history" v-if="flag">
			<view class="search-title">
				<view>搜索历史</view>
				<view @click="clear"><image src="../../static/coen/searchend.svg" mode="widthFix"></image></view>
			</view>
			<!-- tab -->
			<view class="menu-block">
				<block v-for="(item, index) in setdata" :key="index">
					<view @click="research(item)">{{ item }}</view>
				</block>
			</view>
		</view>

		<!-- 内容展示 -->
		<view class="content-left" v-if="content">
			<block v-for="(item, index) in takeshop" :key="index">
				<view class="content-view">
					<view class="content-img"><image :src="item.logo" mode="aspectFill"></image></view>
					<view class="content-title">
						<text class="conteng-take">{{ item.shop }}</text>
						<view class="conteng-monthly">
							<text>月售200</text>
							<text>约{{ item.duration }}分钟</text>
						</view>
						<view class="conteng-starting">
							<text>起送¥{{ item.delivering }}</text>
							<text>配送¥{{ item.physical }}</text>
							<text>人均¥{{ item.capita }}</text>
						</view>
						<view class="conteng-starting">
							<image src="../../static/coen/liwu.svg" mode="widthFix"></image>
							<text>{{ item.types }}</text>
						</view>
					</view>
				</view>
			</block>
		</view>

		<!-- 提示组件 -->
		<tips ref="tip"></tips>
	</view>
</template>

<script>
// 导入封装的请求方法
import { publicing } from '../../api/api.js';

// 导入提示组件
import tips from '../../toast/tips.vue';

// 导入请求地址
import { searchurl } from '../../api/request.js';
export default {
	data() {
		return {
			// 搜索关键词
			searchdata: '',
			// 搜索结果
			takeshop: [],
			// 搜索历史
			setdata: [],
			// 是否显示搜索历史
			flag: false,
			// 主内容区
			content:false
		};
	},
	created() {
		this.getStorageSync();
	},
	methods: {
		// 点击搜索
		getsearchdata() {
			console.log(this.searchdata);
			// let searchkey=this.searchdata

			if (this.searchdata != '') {
				this.getsearchdataP(this.searchdata);
				this.setStorageSync(this.searchdata);
				this.flag = false;
			} else {
				uni.showToast({
					title: '内容不能为空',
					duration: 2000,
					icon: 'none'
				});
			}
		},
		// 回车搜索
		onKeyInput(e) {
			console.log(e.detail.value);
			// let searchkey=this.searchdata

			if (e.detail.value != '') {
				this.getsearchdataP(e.detail.value);
				this.setStorageSync(e.detail.value);
				this.flag = false;
			} else {
				uni.showToast({
					title: '内容不能为空',
					duration: 2000,
					icon: 'none'
				});
			}
		},
		// 发送POST数据请求
		getsearchdataP(searchkey) {
			let data = {
				searchdata: searchkey
			};
			publicing(searchurl, data)
				.then(res => {
					console.log(res);
					// this.takeshop = res.data;
					// 判断一下是否取到数据
					if (res.data == '没有商品数据') {
						console.log(res.data);
						let bull = true;
						let tips = '没有你要的商品';
						this.getTips(bull, tips);
						this.content = false
					} else {
						this.takeshop = res.data;
						this.content = true
						let bull = false;
						let tips = '没有你要的商品';
						this.getTips(bull, tips);
					}
				})
				.catch(err => {
					console.log(err);
				});
		},
		// 搜索历史（存入）
		setStorageSync(history) {
			let historyarr = uni.getStorageSync('search_key') || [];
			historyarr.unshift(history);
			// 将搜索历史存入本地
			uni.setStorageSync('search_key', historyarr);
			// 数组去重
			// let clearArr=Array.from(new Set(historyarr))//ES6的去重方法
			// this.setdata=clearArr
		},
		// 搜索历史（取出）
		getStorageSync() {
			let historyarr = uni.getStorageSync('search_key');
			// 数组去重
			let clearArr = Array.from(new Set(historyarr)); //ES6的去重方法
			this.setdata = clearArr;
			if (clearArr == '') {
				this.flag = false;
			} else {
				this.flag = true;
			}
		},
		// 清除搜索历史
		clear() {
			uni.removeStorageSync('search_key');
			this.getStorageSync();
		},
		// 提示组件
		getTips(bull, tips) {
			this.$refs.tip.init(bull, tips);
		},
		// 点击搜索历史自动搜索
		research(item){
			console.log(item)
			this.getsearchdataP(item)
			this.flag=false
		}
	},
	components: {
		tips
	}
};
</script>

<style scoped>
@import '../../common/takeout.css';

.content-left {
	margin: 0 20upx;
}
.search {
	height: 70upx;
	line-height: 70upx;
	width: 100%;
	display: flex;
	flex-direction: row;
	background: #f8f8f8;
	border-radius: 20upx;
	margin-left: 20upx;
}
.search input {
	height: 70upx;
	line-height: 70upx;
	width: 100%;
	font-size: 30upx;
	color: #666666;
	padding-left: 30upx;
}
.search-cont {
	display: flex;
	justify-content: space-between;
	height: 70upx;
	align-items: center;
	padding: 30upx 0;
}
.search-code {
	width: 150upx;
	height: 50upx;
	text-align: center;
	font-size: 30upx;
}
/* 搜索历史 */
.search-history {
	margin: 20upx;
}
.search-title {
	font-size: 30upx;
	font-weight: bold;
	display: flex;
	justify-content: space-between;
	align-items: center;
	height: 60upx;
	line-height: 60upx;
}
.search-title image {
	width: 36upx;
	height: 36upx;
	display: block;
}
.menu-block view {
	background: #f7f8fa;
	border-radius: 6upx;
	font-size: 27upx;
	color: #292c33;
	text-align: center;
	padding: 10upx;
	margin: 20upx 20upx 0 0;
}

.menu-block {
	display: flex;
	flex-direction: row;
	justify-content: flex-start;
	flex-wrap: wrap;
}
</style>
